import java.util.*;

public class Test {
    //测试map的方法
    //map不能实现迭代器,因为它都没有实现那个接口
    public static void main1(String[] args) {
        Map<String,Integer> map=new TreeMap<>();
        map.put("abc",1);
        map.put("abcd",2);
        map.put("hello",3);
        System.out.println(map);
        System.out.println(map.getOrDefault("abcd", 0));
        System.out.println(map.getOrDefault("ahdhad", 0));//并不会添加到map里，如果没有的话
        System.out.println(map);

        map.remove("abc");
        System.out.println(map);

        Set<String> set=map.keySet();
        Collection<Integer> values = map.values();
        System.out.println(values);
        System.out.println(set);

        Set<Map.Entry<String,Integer>> set1=map.entrySet();
        System.out.println(set1);
        for(Map.Entry<String,Integer> entry:set1){
            System.out.println("key: "+entry.getKey()+" value: "+entry.getValue());
        }

        if(map.containsKey("abcd")){
            System.out.println("存在abcd");
        }
        if(map.containsValue(2)){
            System.out.println("存在值2");
        }
    }

    //测试set的方法
    public static void main(String[] args) {
        Set<String> set=new TreeSet<>();
        set.add("abc");
        set.add("abcd");
        set.add("abcde");
        System.out.println(set);
        System.out.println(set.size());
        Iterator<String> iterator= set.iterator();
        while(iterator.hasNext()){
            System.out.println(iterator.next());
        }

        if(set.contains("abc")){
            System.out.println("存在abc");
        }
        set.remove("abc");
        System.out.println(set);

        String[] s=set.toArray(new String[0]);
        for (int i = 0; i < 2; i++) {
            System.out.println(s[i]);
        }

        set.clear();
        System.out.println(set);

    }
}
